﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

namespace Nirvana
{
    [CustomEditor(typeof(LuaCheckRule), true)]
    public class LuaCheckRuleEditor1 : Editor
    {
        private SerializedProperty includeRegexs;
        private SerializedProperty excludeRegexs;
        private SerializedProperty luaStd;
        private SerializedProperty noGlobal;
        private SerializedProperty globals;
        private SerializedProperty readGlobals;
        private SerializedProperty noUnused;
        private SerializedProperty noRedefined;
        private SerializedProperty noUnusedArgs;
        private SerializedProperty noUnusedSecondaries;
        private SerializedProperty noSelf;
        private SerializedProperty maxCodeLineLength;
        private SerializedProperty maxStringLineLength;
        private SerializedProperty maxCommentLineLength;
        private SerializedProperty ignores;

        private int startindex = -1;
        private Vector2 pos;
        private static bool isExpanded = true;

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.includeRegexs = serializedObject.FindProperty("includeRegexs");
            this.excludeRegexs = serializedObject.FindProperty("excludeRegexs");
            this.luaStd = serializedObject.FindProperty("luaStd");
            this.noGlobal = serializedObject.FindProperty("noGlobal");
            this.globals = serializedObject.FindProperty("globals");
            this.readGlobals = serializedObject.FindProperty("readGlobals");
            this.noUnused = serializedObject.FindProperty("noUnused");
            this.noRedefined = serializedObject.FindProperty("noRedefined");
            this.noUnusedArgs = serializedObject.FindProperty("noUnusedArgs");
            this.noUnusedSecondaries = serializedObject.FindProperty("noUnusedSecondaries");
            this.noSelf = serializedObject.FindProperty("noSelf");
            this.maxCodeLineLength = serializedObject.FindProperty("maxCodeLineLength");
            this.maxStringLineLength = serializedObject.FindProperty("maxStringLineLength");
            this.maxCommentLineLength = serializedObject.FindProperty("maxCommentLineLength");
            this.ignores = serializedObject.FindProperty("ignores");
        }
        public override void OnInspectorGUI()
        {
           // base.serializedObject.Update();
            GUILayout.Label("LuaCheck说明", GUITheme.LabelTitleStyle);
            EditorGUILayout.HelpBox("\n" +
            "Luacheck是一个静态Lua代码分析器，用于检测Lua脚本中的各种问题，\n" +
            "诸如：使用未定义的全局变量、未使用的变量或值、试图使用未初始化的变量、无法到达的代码等等\n", MessageType.Info);
            EditorGUILayout.Space();

            GUI.color = Color.cyan;
            EditorGUILayout.HelpBox("参数说明（点击这里打开)：http://luacheck.readthedocs.io/en/stable/", MessageType.Info);
            Rect lastRect = GUILayoutUtility.GetLastRect();
            if (Event.current.button == 0 && Event.current.type == EventType.MouseUp && lastRect.Contains(Event.current.mousePosition))
            {
                Help.BrowseURL("http://luacheck.readthedocs.io/en/stable/");
            }
            GUI.color = Color.white;
            EditorGUILayout.Space();
            isExpanded = EditorGUILayout.Foldout(isExpanded, "Config");
            if (isExpanded)
            {
                GUILayout.Label("匹配规则", GUITheme.LabelTitleStyle);
                GUILayoutEx.BeginContents();
                GUI.color = Color.yellow;
                EditorGUILayout.HelpBox("注意：规则文件只会检测同一父目录或同一级目录下的资源。\n\n" +
                    "正则例子：1.匹配非空字符：\\S , 2.任意字符：. ", MessageType.None);

                GUI.color = Color.white;
                EditorGUILayout.Space();
                EditorGUILayout.PropertyField(this.includeRegexs);
                EditorGUILayout.PropertyField(this.excludeRegexs);

                GUILayoutEx.EndContents();
                EditorGUILayout.Space(10f);

                DrawLuaStdGUI();

                EditorGUILayout.PropertyField(this.noGlobal);
                EditorGUILayout.PropertyField(this.globals);
                EditorGUILayout.PropertyField(this.readGlobals);
                EditorGUILayout.PropertyField(this.noUnused);
                EditorGUILayout.PropertyField(this.noRedefined);
                EditorGUILayout.PropertyField(this.noUnusedArgs);
                EditorGUILayout.PropertyField(this.noUnusedSecondaries);
                EditorGUILayout.PropertyField(this.noSelf);
                EditorGUILayout.PropertyField(this.maxCodeLineLength);
                EditorGUILayout.PropertyField(this.maxStringLineLength);
                EditorGUILayout.PropertyField(this.maxCommentLineLength);
                EditorGUILayout.PropertyField(this.ignores);

                base.serializedObject.ApplyModifiedProperties();
            }



            LuaCheckRule assetCheckRule = (LuaCheckRule)this.target;
            EditorGUILayout.Space(10f);
            if (GUILayoutEx.ActionButton("Check"))
            {

                using (ProgressIndicator progressIndicator = new ProgressIndicator("Check Lua"))
                {
                    progressIndicator.SetTotal((float)assetCheckRule.GetTotalCount());
                    assetCheckRule.Progress(progressIndicator);
                    //  EditorUtility.SetDirty(this.target);
                }
                isExpanded = assetCheckRule.Reports.Count == 0;//隐藏参数窗口
                GUIUtility.ExitGUI();
            }

            EditorGUILayout.Space();
            var list = assetCheckRule.Reports;
            if (list.Count > 0)
            {
                GUI.color = Color.yellow;
                EditorGUILayout.PrefixLabel(string.Format("Report ({0}):", list.Count));
                GUI.color = Color.white;
                EditorGUILayout.Space();
                this.pos = LuaCheckReportItem.Show(list, this.pos, ref this.startindex);
            }
           
        }


        private void DrawLuaStdGUI()
        {

            GUILayout.Label("设置标准全局变量", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents();
            EditorGUILayout.HelpBox("max- Lua 5.1，Lua 5.2，Lua 5.3和LuaJIT 2.x的全局并集;\n" +
                "min- Lua 5.1、Lua 5.2、Lua 5.3 和 LuaJIT 2.x 的全局数的交集;\n" +
                "lua51- Lua 5.1的全局变量，没有弃用的全局变量;\n" +
                "lua51c- Lua 5.1的全局变量;\n" +
                "lua52- Lua 5.2的全局变量;\n" +
                "lua52c- Lua 5.2的全局数据，用LUA_COMPAT_ALL编译;\n" +
                "lua53- Lua 5.3的全局;\n" +
                "lua53c- Lua 5.3的全局变量，用LUA_COMPAT_5_2编译;\n" +
                "lua54- Lua 5.4的全局;\n" +
                "lua54c- Lua 5.4的全局数据，使用LUA_COMPAT_5_3编译;\n" +
                "luajit- LuaJIT 2.x的全局变量;\n" +
                "ngx_lua- Openresty lua-nginx-module 0.10.10的全局变量，包括标准的LuaJIT 2.x全局变量;\n" +
                "love-由 LÖVE添加的全局变量;\n" +
                "busted- 由 Busted 2.0 添加的全局变量\n" +
                "luacheckrc- Luacheck 配置中允许的全局变量，默认情况下为以下列结尾的文件添加.luacheckrc;\n" +
                "none- 没有标准的全局变量。" , MessageType.None);

            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.luaStd);

            GUILayoutEx.EndContents();
            EditorGUILayout.Space(10f);

        }

    }
}

